package com.zhangyuji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangyuji.common.MyCurrentId;
import com.zhangyuji.common.R;
import com.zhangyuji.dto.SetmealDto;
import com.zhangyuji.entity.Category;
import com.zhangyuji.entity.Setmeal;
import com.zhangyuji.service.CategoryService;
import com.zhangyuji.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Resource
    private SetmealService setmealService;

    @Resource
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<IPage> page(int page, int pageSize, String name){
        log.info("page={} pageSize={} name={}",page,pageSize,name);
        IPage<Setmeal> iPage = new Page<>(page,pageSize);
        IPage<SetmealDto> dtoIPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Setmeal::getName,name)
                    .orderByAsc(Setmeal::getPrice);
        iPage = setmealService.page(iPage, queryWrapper);
        BeanUtils.copyProperties(iPage,dtoIPage,"records");
        //
        log.info("dtoIPage = {}",dtoIPage);

        BeanUtils.copyProperties(iPage.getRecords(),dtoIPage.getRecords());

//        System.out.println(dtoIPage.getRecords());
        log.info("dtoIPage = {}",dtoIPage.getRecords());
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal record : iPage.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();

            Long categoryId = record.getCategoryId();
            //根据categoryId查询name
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            log.info("categoryName = {}",categoryName);
            BeanUtils.copyProperties(record,setmealDto);
            setmealDto.setCategoryName(categoryName);

            setmealDtoList.add(setmealDto);
        }
        dtoIPage.setRecords(setmealDtoList);
        log.info("dtoIPage ={}",dtoIPage);
        return dtoIPage != null ? R.success(dtoIPage) : R.error("查询失败");
    }

    /**
     * 修改 停售/起售 状态
     * @param status
     * @param ids
     * @param request
     * @return
     */

    @PostMapping("/status/{status}")
    public R<String> setStatus(@PathVariable Integer status, Long[] ids , HttpServletRequest request){
        log.info("status:{},ids={}",status,ids.toString());
        List<Setmeal> setmealList = new ArrayList<>();

        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealList.add(setmeal);
        }
        MyCurrentId.setCurrentId((Long) request.getSession().getAttribute("employee"));
        boolean b = setmealService.updateBatchById(setmealList);
        return b ? R.success("修改状态成功") : R.error("修改状态失败");
    }

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */

    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto,HttpServletRequest request){

        log.info("setmealDish={}",setmealDto);
        MyCurrentId.setCurrentId((Long) request.getSession().getAttribute("employee"));
        boolean flag = setmealService.addSetmealAndSetmealDish(setmealDto);
        return flag ? R.success("添加成功") : R.error("添加失败");
    }

    /**
     * 单个删除/批量删除
     * @param ids
     * @return
     */

    @DeleteMapping
    public R<String> deleteSetmeal(Long[] ids){

        log.info("ids={}",ids);
        boolean flag = setmealService.deleteSetmealAndSetmealDish(ids);

        return flag ? R.success("删除成功") : R.error("删除失败");
    }

    @PutMapping
    public R<String> editSetmeal(@RequestBody SetmealDto setmealDto,HttpServletRequest request){
        log.info("setmealDto = {}",setmealDto);
        MyCurrentId.setCurrentId((Long) request.getSession().getAttribute("employee"));
        boolean flag = setmealService.editSetmeal(setmealDto);
        return flag ? R.success("修改成功") : R.error("修改失败");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> querySetmealById(@PathVariable Long id){
        log.info("querySetmealById=== id=={}",id);
        SetmealDto setmealDto = setmealService.querySetmealById(id);
        return setmealDto != null ? R.success(setmealDto) : R.error("查询失败");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> getSetmealList(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId())
                    .eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return R.success(setmealList);
    }
}
